<?php
use Think\Log;

/**
 * 以易于阅读的方式打印php数组
 * @param $data
 */
function p($data)
{
    // 定义样式
    $str = '<pre style="display: block;padding: 9.5px;margin: 44px 0 0 0;font-size: 13px;line-height: 1.42857;color: #333;word-break: break-all;word-wrap: break-word;background-color: #F5F5F5;border: 1px solid #CCC;border-radius: 4px;">';
    // 如果是boolean或者null直接显示文字；否则print
    if (is_bool($data)) {
        $show_data = $data ? 'true' : 'false';
    } elseif (is_null($data)) {
        $show_data = 'null';
    } else {
        $show_data = print_r($data, true);
    }
    $str .= $show_data;
    $str .= '</pre>';
    echo $str;
}

//计算月份差
function getMonthNum($date1, $date2, $tags = '-')
{
    $date1 = explode($tags, $date1);
    $date2 = explode($tags, $date2);
    return abs($date1[0] - $date2[0]) * 12 + abs($date1[1] - $date2[1]);
}

function fc_get_add_month($datetime, $month = 1)
{
    if (empty($datetime)) {
        return "";
    }
    if ($month < 0) {
        $h = date("Y-m", strtotime('-' . $month . " month", strtotime($datetime)));
    } else {
        $h = date("Y-m", strtotime('+' . $month . " month", strtotime($datetime)));
    }
    return $h;
}

function get_month_first_day($date = "", $format = false)
{
    if (empty($date)) {
//        $date = time();
        $date = date('Y-m-01', time());
    } else {
        $date = date('Y-m-01', strtotime($date));
    }
    if (!$format) {
        return strtotime($date);
    } else {
        return $date;
    }
}

function get_next_month_first_day($date, $format = false)
{
    $timestamp = strtotime(date('Y-m-01', strtotime($date)) . ' +1 month');
    if ($format) {
        return date('Y-m-d', $timestamp);
    } else {
        return $timestamp;
    }
}

function get_month_last_day($date, $format = false)
{
    $timestamp = strtotime(date('Y-m-01', strtotime($date)) . ' +1 month -1 day');
    if ($format) {
        return date('Y-m-d', $timestamp);
    } else {
        return $timestamp;
    }
}

function fc_get_date_describe($datetime)
{
    if (empty($datetime)) {
        return "";
    }

    $date1 = date_create(date('y-m-d ', time()));
    $date2 = date_create(date('Y-m-d', strtotime($datetime)));
    $diff = date_diff($date1, $date2);
    if ($diff) {
        $days = $diff->format("%r%a");
        if ($days == 0) {
            return "今天";
        } else if ($days == 1) {
            return "明天";
        } else if ($days == 2) {
            return "后天";
        } else if ($days == -1) {
            return "昨天";
        } else if ($days == -2) {
            return "前天";
        } else {
            return date('Y-m-d', strtotime($datetime));
        }
    } else {
        return "";
    }
}

function fc_get_add_day($datetime, $day = 1)
{
    if (empty($datetime)) {
        return "";
    }
    if ($day < 0) {
        $h = date("Y-m-d", strtotime('-' . $day . " day", strtotime($datetime)));
    } else {
        $h = date("Y-m-d", strtotime('+' . $day . " day", strtotime($datetime)));
    }
    return $h;
}

function fc_get_date_diff($datetime1, $datetime2 = null)
{
    if (empty($datetime1)) {
        return "";
    }

    if (empty($datetime2)) {
        $date1 = date_create(date('y-m-d ', time()));
    } else {
        $date1 = date_create(date('y-m-d ', strtotime($datetime2)));
    }
    $date2 = date_create(date('Y-m-d', strtotime($datetime1)));
    $diff = date_diff($date1, $date2);
    return $days = $diff->format("%r%a");
}

function fc_get_date_time($datetime)
{
    if (empty($datetime)) {
        return "";
    }

    $h = date("H:m", strtotime($datetime));
    return $h;
}

//===============================================================================================
//============================================业务相关============================================
//===============================================================================================

//获取用户名
function fc_get_username($userid)
{
    if (empty($userid)) {
        return "";
    }
    $userlist = S("userlist");
    foreach ($userlist as $vo) {
        if ($userid == $vo['id']) {
            return $vo['name'];
        }
    }
    return "";
}

function fc_get_menu_by_path($menus, $path)
{
    if (count($menus) > 0) {
        foreach ($menus as $menu) {
            if (stripos($path, $menu->get_data('path')) !== false) {
                return $menu;
            } else {
//                return $menu;
            }
            $child = $menu->get_child();
            if (count($child) > 0) {
                $result = fc_get_menu_by_path($child, $path);
                if ($result != null) {
                    return $result;
                }
            }
        }
    }
    return null;
}

function fc_search_menu_by_path($menus, $path)
{
    foreach ($menus as $menu) {
        if ($menu['path'] === $path) {
            return $menu;
        }
    }
    return null;
}

function fc_search_department_by_user($departs, $uid)
{
    if (count($departs) > 0) {
        foreach ($departs as $depart) {
            $users = $depart['sys_user'];
            if (count($users) > 0) {
                foreach ($users as $user) {
                    if ($user['id'] == $uid) {
                        return $depart;
                    }
                }
            }

            if (count($depart['_child']) > 0) {
                $result = fc_search_department_by_user($depart['_child'], $uid);
                if ($result != null) {
                    return $result;
                }
            }
        }
    }
    return null;
}

function fc_get_depart($uid)
{
    $departs = fc_get_department_tree();
    $depart = fc_search_department_by_user($departs, $uid);
    return $depart;
}

function fc_get_manager($uid)
{
    $depart = fc_get_depart($uid);
    if ($depart != null) {
        $users = $depart['sys_user'];
        if (count($users) > 0) {
            foreach ($users as $user) {
                if ($user['role'] == 2) {
                    return $user;
                }
            }
        }
    }
    return null;
}

function fc_get_employee_ids($depart, $includeManage = true)
{
    $ids = [];
    if ($depart != null) {
        $users = $depart['sys_user'];

        if (count($users) > 0) {
            foreach ($users as $user) {
                if ($user['role'] == 1 || ($includeManage && $user['role'] == 2)) {
                    $ids[] = $user['id'];
                }
            }
        }

        if (count($depart['_child']) > 0) {
            foreach ($depart['_child'] as $depart_child) {
                $result = fc_get_employee_ids($depart_child, $includeManage);
                if (count($result) > 0) {
                    $ids = array_merge($ids, $result);
                }
            }
        }
    }
    return $ids;
}

function fc_get_employee_ids_by_uid($uid)
{
    $depart = fc_get_depart($uid);
    $uids = fc_get_employee_ids($depart);
    return $uids;
}

function fc_config_name_by_key($config_name, $key)
{
    if ($key != '') {
//    Log::write(sprintf('$config_name is %s ,$key is %s', $config_name, $key), 'WARN');
        $config = C($config_name);
        return $config[$key];
    } else {
        return '';
    }
}